<?php

namespace Admin\Controllers;

use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;
use Models\Topics;
use Models\Doctypes;

class ControllerBase extends Controller {

    public function initialize() {
        $doctypes = Doctypes::find()->toArray();
        foreach ($doctypes as $key => $type) {
            $doctypes[$key]['topics'] = Topics::find("doctype_id = '" . $type['id'] . "'")->toArray();
        }
        $this->view->title = 'VIDS';
        $this->view->menu = $doctypes;
        $this->view->text_search = '';
    }

    public function beforeExecuteRoute(Dispatcher $dispatcher) {
        $controllerName = $dispatcher->getControllerName();
        $actionName = $dispatcher->getActionName();

        $identity = $this->auth->getIdentity();
        if ($controllerName != 'adminerrorlogin') {
            if ($identity['profile'] != 2 && $identity['profile'] != 3) {
                $this->response->redirect('admin/accessdenied');
            } else {
//                $dispatcher->forward(array(
//                    'module' => 'admin',
//                    'controller' => $controllerName,
//                    'action' => $actionName,
//                ));
            }
        }
//        if ($this->acl->isPrivate($dispatcher->getModuleName(), $controllerName)) {
//            $actionName = $this->getActionName($dispatcher->getActionName());
//
//            if (!$this->acl->isAllowed($identity['profile'], $dispatcher->getModuleName() . $controllerName, $actionName)) {
//
//                $this->flash->error('You don\'t have access to this module: ' . $controllerName . ':' . $actionName);
//
//                if ($this->acl->isAllowed($identity['profile'], $controllerName, 'index')) {
//                    $dispatcher->forward(array(
//                        'module' => 'admin',
//                        'controller' => $controllerName,
//                        'action' => 'index'
//                    ));
//                } else {
//                    $dispatcher->forward(array(
//                        'module' => 'admin',
//                        'controller' => 'error',
//                        'action' => 'index'
//                    ));
//                }
//                return false;
//            }
//        }
        $this->view->auth = $identity;
    }

}
